<?php
/**
 * Header controller, which is used in most page at front shop.
 * @author Kota Kakaji
 *
 */
class ControllerCommonHeader extends Controller {
	/**
	 * Index fucntion
	 */
	protected function index() {
		//Controller id
		$this->id = 'header';
		
    	if (($this->request->server['REQUEST_METHOD'] == 'POST') && isset($this->request->post['language_code'])) {
			$this->session->data['language'] = $this->request->post['language_code'];

			if (isset($this->request->post['redirect'])) {
				$this->redirect($this->request->post['redirect']);
			} else {
				$this->redirect(HTTP_SERVER . 'index.php?route=common/home');
			}
    	}
		
    	//Shop name variable
		$shop_name = "";
		
		//User name variable
		$user_name = "";
		
		//Load model shop
		$this->load->model('upload/shop');
		
		//Get customer information
		$customer_id = $this->customer->getId();
		$user_name   = $this->customer->getUserName();
		
		//Get customer's shop information
		$shopInfo     = $this->model_upload_shop->getShopIdByCustomerId($customer_id);

		if (($this->request->server['REQUEST_METHOD'] == 'POST') && isset($this->request->post['currency_code'])) {
      		$this->currency->set($this->request->post['currency_code']);

			unset($this->session->data['shipping_methods']);
			unset($this->session->data['shipping_method']);

			if (isset($this->request->post['redirect'])) {
				$this->redirect($this->request->post['redirect']);
			} else {
				$this->redirect(HTTP_SERVER . 'index.php?route=common/home');
			}
   		}

		$this->language->load('common/header');

		if (isset($this->request->server['HTTPS']) && (($this->request->server['HTTPS'] == 'on') || ($this->request->server['HTTPS'] == '1'))) {
			$server = HTTPS_IMAGE;
		} else {
			$server = HTTP_IMAGE;
		}

		$this->data['title'] = $this->document->title;
		$this->data['description'] = $this->document->description;

		if (isset($this->request->server['HTTPS']) && (($this->request->server['HTTPS'] == 'on') || ($this->request->server['HTTPS'] == '1'))) {
			$this->data['base'] = HTTPS_SERVER;
		} else {
			$this->data['base'] = HTTP_SERVER;
		}

		if ($this->config->get('config_icon') && file_exists(DIR_IMAGE . $this->config->get('config_icon'))) {
			$this->data['icon'] = $server . $this->config->get('config_icon');
		} else {
			$this->data['icon'] = '';
		}

		$this->data['charset'] = $this->language->get('charset');
		$this->data['lang'] = $this->language->get('code');
		$this->data['direction'] = $this->language->get('direction');
		$this->data['links'] = $this->document->links;
		$this->data['styles'] = $this->document->styles;
		$this->data['scripts'] = $this->document->scripts;
		$this->data['breadcrumbs'] = $this->document->breadcrumbs;

		$this->data['store'] = $this->config->get('config_name');

		if ($this->config->get('config_logo') && file_exists(DIR_IMAGE . $this->config->get('config_logo'))) {
			$this->data['logo'] = $server . $this->config->get('config_logo');
		} else {
			$this->data['logo'] = '';
		}
		
		//Language data use in template file
		$this->data['text_home'] = $this->language->get('text_home');
		$this->data['text_special'] = $this->language->get('text_special');
		$this->data['text_contact'] = $this->language->get('text_contact');
		$this->data['text_sitemap'] = $this->language->get('text_sitemap');
		$this->data['text_bookmark'] = $this->language->get('text_bookmark');
    	$this->data['text_account'] = $this->language->get('text_account');
    	$this->data['text_login'] = $this->language->get('text_login');
    	$this->data['text_logout'] = $this->language->get('text_logout');
    	$this->data['text_cart'] = $this->language->get('text_cart');
    	$this->data['text_checkout'] = $this->language->get('text_checkout');
		$this->data['text_keyword'] = $this->language->get('text_keyword');
		$this->data['text_category'] = $this->language->get('text_category');
		$this->data['text_advanced'] = $this->language->get('text_advanced');
		$this->data['entry_search'] = $this->language->get('entry_search');
		$this->data['button_go'] = $this->language->get('button_go');

		$this->data['text_checkout'] = $this->language->get('text_checkout');
    	$this->data['text_uploadtosell'] = $this->language->get('text_uploadtosell');
    	$this->data['text_register'] = $this->language->get('text_register');
    	$this->data['text_shop']     = $this->language->get('text_shop');
    	$this->data['text_hello']    = $this->language->get('text_hello');
    	$this->data['user_id']       = $user_name;
		
    	//Link used in template files
		$this->data['home'] = HTTP_SERVER . 'index.php?route=common/home';
		$this->data['special'] = HTTP_SERVER . 'index.php?route=product/special';
		$this->data['contact'] = HTTP_SERVER . 'index.php?route=about/contact';
		$this->data['introduce'] = HTTP_SERVER . 'index.php?route=about/introduce';
		
		$this->data['recruitment'] = HTTP_SERVER . 'index.php?route=about/recruitment';
    	$this->data['sitemap'] = HTTP_SERVER . 'index.php?route=information/sitemap';
    	$this->data['account'] = HTTPS_SERVER . 'index.php?route=account/account';
		$this->data['logged'] = $this->customer->isLogged();
		$this->data['login'] = HTTPS_SERVER . 'index.php?route=account/login';
		$this->data['logout'] = HTTP_SERVER . 'index.php?route=account/logout';
    	$this->data['cart'] = HTTP_SERVER . 'index.php?route=checkout/cart';
		$this->data['checkout'] = HTTPS_SERVER . 'index.php?route=checkout/shipping';

		if (isset($this->request->get['keyword'])) {
			$this->data['keyword'] = $this->request->get['keyword'];
		} else {
			$this->data['keyword'] = '';
		}

		if (isset($this->request->get['category_id'])) {
			$this->data['category_id'] = $this->request->get['category_id'];
		} elseif (isset($this->request->get['path'])) {
			$path = explode('_', $this->request->get['path']);

			$this->data['category_id'] = end($path);
		} else {
			$this->data['category_id'] = '';
		}

		$this->data['advanced'] = HTTP_SERVER . 'index.php?route=product/search';

		$this->load->model('catalog/category');

		$this->data['categories'] = $this->getCategories(0);

		$this->data['action'] = HTTP_SERVER . 'index.php?route=common/home';

		if (!isset($this->request->get['route'])) {
			$this->data['redirect'] = HTTP_SERVER . 'index.php?route=common/home';
		} else {
			$this->load->model('tool/seo_url');

			$data = $this->request->get;

			unset($data['_route_']);

			$route = $data['route'];

			unset($data['route']);

			$url = '';

			if ($data) {
				$url = '&' . urldecode(http_build_query($data));
			}

			$this->data['redirect'] = $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=' . $route . $url);
		}
	
		if(empty($customer_id) == true){
			$this->data['register'] = $this->url->https('account/create');
		}
		$this->data['uploadtosell'] = $this->url->https('upload/upload');
		if(empty($shopInfo) == false)
		{
			$shop_name = $shopInfo[0]["shop_name"];
		}
		$this->data['linkshop'] = $this->url->https('shops/shop');
		$this->data['shopname'] = $shop_name;

		//Set template files
		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/common/header.phtml')) {
			$this->template = $this->config->get('config_template') . '/common/header.phtml';
		} else {
			$this->template = 'default/template/common/header.phtml';
		}
		
		//Render
    	$this->render();
	}
	
	/**
	 * Get all children category of an category, base on its id and begin from an level.
	 * @param int $parent_id id of parent category
	 * @param int $level level begin from
	 * @return array of category information
	 */
	private function getCategories($parent_id, $level = 0) {
		//Level
		$level++;
		
		//Result
		$data = array();
		
		//Get all child category of parent category
		$results = $this->model_catalog_category->getCategories($parent_id);

		foreach ($results as $result) {
			$data[] = array(
				'category_id' => $result['category_id'],
				'name'        => str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;', $level) . $result['name']
			);
			//Continue to get child category
			$children = $this->getCategories($result['category_id'], $level);
			
			//Merge result
			if ($children) {
			  $data = array_merge($data, $children);
			}
		}
	
		return $data;
	}
}
?>